package com.functest.orm

import com.functest.framework.BaseFunctionalTest
import org.testng.Assert
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: KovalevAS
 * Date: 28.06.13
 * Time: 16:20
 * To change this template use File | Settings | File Templates.
 */
class SingleDbQueryBuilderTest extends BaseFunctionalTest {
    @Test
    void SingleDbQueryBuilder_field() {
        def single_mapper = TestData.student_table_full_mapper
        def single_multicolumn_mapper = TestData.marks_table_multi_column_mapper
        def multitable_mapper = TestData.multiTableMapper_without_bindings

        def mappers = [single_mapper, single_multicolumn_mapper, multitable_mapper]
        def mapper
        def fields

        SingleDbQueryBuilder single_db_query_builder = null
        mappers.each {
            mapper = it

            if (mapper != multitable_mapper)
                single_db_query_builder = new SingleDbQueryBuilder((mapper as SingleDbTableMapper), (mapper as SingleDbTableMapper).beanClass)
            else
                single_db_query_builder = new SingleDbQueryBuilder(mapper as MultiTableMapper)

            String first_field = mapper.allFields.first().name
            String second_field = mapper.allFields.last().name
            String same_field = mapper.allFields.last().name
            String defunct_field = 'defunctField'
            def blank_field = null
            def usedFields = 2

            fields = [first_field, second_field, same_field, defunct_field, blank_field]

            fields.each
                    {
                        if (it == defunct_field) {
                            try {
                                single_db_query_builder = single_db_query_builder.field(it)
                                Assert.fail "Error: NoSuchFieldException expected for $it!"
                            }
                            catch (NoSuchFieldException ex) {
                                assert ex.message.contains('No such field')
                            }
                        } else if (it == blank_field) {
                            try {
                                single_db_query_builder = single_db_query_builder.field(it)
                                Assert.fail 'Error: NullPointerException expected!'
                            }  catch (NullPointerException ex) {
                                assert ex.message.contains('null object')
                            }
                        } else single_db_query_builder = single_db_query_builder.field(it)
                    }
            assert single_db_query_builder.usedFields.size() == usedFields
            assert single_db_query_builder.usedMappers
            assert single_db_query_builder.usedSourceFqNames
            single_db_query_builder.usedSourceFqNames.each
                    {
                        assert single_db_query_builder.columnsList.contains(it)
                    }
        }
    }
}
